package com.nd.mobile.study.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.nd.mobile.study.R;
import com.nd.mobile.study.common.App;
import com.nd.mobile.study.common.BaseActivity;
import com.nd.mobile.study.common.SimpleAsyncTask;
import com.nd.mobile.study.common.constants.BundleConstants;
import com.nd.mobile.study.domain.MaterialInfo;
import com.nd.mobile.study.domain.Subject;
import com.nd.mobile.study.view.adapter.SimpleListAdapter;

public class MaterialInfoCatalogActivity extends BaseActivity {
	private ListView				mLV;
	private MaterialCatalogAdapter	mAdapter;
	private MaterialInfo[]			mMaterialInfos;
	private Intent					mIntent2Go;
	private Subject					mSubject;
	private TextView				mTVListTitle;

	@Override
	protected void onBaseCreate(Bundle savedInstanceState) {
		setContentView(R.layout.class_info);
	}

	@Override
	protected void initViews() {
		super.initHeader();
		mHeaderTitleTv.setText(R.string.tv_app_studydocu);
		mHeaderBtnHome.setVisibility(View.VISIBLE);
		mLV = (ListView) findViewById(R.id.common_lv);
		mTVListTitle = (TextView) findViewById(R.id.tv_list_title);
		mTVListTitle.setText("选择资料");
	}

	@Override
	protected void setFieldValues() {
		mIntent2Go = new Intent().setClass(this, MaterialInfoDetailActivity.class);
		mSubject = (Subject) getIntent().getSerializableExtra(BundleConstants.SUBJECT_INFO);
	}

	@Override
	protected void onAfterCreate(Bundle savedInstanceStat) {
		getMaterialInfo();
	}

	private void getMaterialInfo() {
		mLoading.p();
		new GetMaterialInfoTask(this).execute();
	}

	private class GetMaterialInfoTask extends SimpleAsyncTask<Void, Void, MaterialInfo[]> {

		public GetMaterialInfoTask(Context context) {
			super(context);
		}

		@Override
		protected MaterialInfo[] onLoad(Void... params) throws Exception {
			return mSubject.getMaterialInfoCatalog(App.getInstance().getUser());
		}

		@Override
		protected void onUI(MaterialInfo[] result) {
			mMaterialInfos = result;
			mAdapter = new MaterialCatalogAdapter(mContext, mMaterialInfos);
			mLV.setAdapter(mAdapter);
			mLoading.v();
			mLV.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					MaterialCatalogAdapter adapter = (MaterialCatalogAdapter) parent.getAdapter();
					mIntent2Go.putExtra(BundleConstants.MATERIAL_INFO, adapter.getData().get(position));
					startActivity(mIntent2Go);
				}
			});
		}

	}

	private class MaterialCatalogAdapter extends SimpleListAdapter<MaterialInfo> {

		public MaterialCatalogAdapter(Context context, MaterialInfo[] data) {
			super(context, data);
		}

		private class ViewHolder {
			TextView	tvName;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (null == convertView) {
				convertView = mInflater.inflate(R.layout.class_list_item, null);

				holder = new ViewHolder();
				holder.tvName = (TextView) convertView.findViewById(R.id.class_list_tv_name);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.tvName.setText(mData.get(position).getName());
			return convertView;
		}
	}
}
